/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.service.order;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.cache.ProcessEventTypeRuleCache;
import com.sinodata.bsm.center.cache.ProcessExcludeResRuleCache;
import com.sinodata.bsm.center.cache.ProcessRuleCache;
import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.impl.WorkOrderDaoImpl;
import com.sinodata.bsm.common.vo.ProcessEventTypeRule;
import com.sinodata.bsm.common.vo.ProcessExcludeResRule;
import com.sinodata.bsm.common.vo.ProcessRule;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author dongyushi
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-3-13 上午11:02:09          dongyushi        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class ProcessRuleService {

    @Autowired
    private WorkOrderDaoImpl workOrderDao;

    @Autowired
    private BaseDao baseDao;

    @Autowired
    private ProcessRuleCache processRuleCache;
    @Autowired
    private ProcessEventTypeRuleCache processEventTypeRuleCache;
    @Autowired
    private ProcessExcludeResRuleCache processExcludeResRuleCache;

    public List<ProcessRule> findAllByRes(Long resId) {
        return workOrderDao.findAllByRes(resId);
    }

    public List<ProcessEventTypeRule> getWorkOrderEventTypeByWorkOrderId(Long workOrderId) {
        return workOrderDao.getWorkOrderEventTypeByWorkOrderId(workOrderId);
    }

    public ProcessRule getWorkOrderById(Long id) {
        return (ProcessRule) workOrderDao.findById(ProcessRule.class, id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void add(ProcessRule workOrder) {
        baseDao.save(workOrder);
        processRuleCache.put(workOrder);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void addEventType(List<ProcessEventTypeRule> list) throws Exception {
        baseDao.batchSave(list);
        for (ProcessEventTypeRule p : list) {
            processEventTypeRuleCache.put(p);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void delEventType(List<ProcessEventTypeRule> list) throws Exception {
        Long[] ids = new Long[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ids[i] = list.get(i).getId();
        }
        workOrderDao.batchDeleteWorkOrderEventType(ids);
        for (Long id : ids) {
            processEventTypeRuleCache.remove(id);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void update(ProcessRule workOrder) {
        baseDao.update(workOrder);
        processRuleCache.put(workOrder);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(Long id) {
        workOrderDao.deleteWorkOrderExcludeRes(id);
        workOrderDao.deleteWorkOrderEventType(id);
        workOrderDao.delete(this.getWorkOrderById(id));

        processEventTypeRuleCache.removeByProcessRuleId(id);
        processExcludeResRuleCache.removeByProcessRuleId(id);
        processRuleCache.remove(id);

    }

    public void deleteResExcludeById(Long id) {
        workOrderDao.deleteWorkOrderExcludeResById(id);
        processExcludeResRuleCache.remove(id);
    }

    public boolean checkName(Long resId, String name) {
        if (workOrderDao.getByNameAndResId(resId, name) > 0) {
            return false;
        } else {
            return true;
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void enable(Long id) {
        ProcessRule workOrder = this.getWorkOrderById(id);
        workOrder.setStatus(1);
        baseDao.update(workOrder);
        processRuleCache.put(workOrder);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void disable(Long id) {
        ProcessRule workOrder = this.getWorkOrderById(id);
        workOrder.setStatus(0);
        baseDao.update(workOrder);
        processRuleCache.put(workOrder);
    }

    public List<ProcessExcludeResRule> getWorkOrderExcludeResByWorkOrderId(Long workOrderId) {
        return workOrderDao.getWorkOrderExcludeResByWorkOrderId(workOrderId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void batchAddWorkOrderExcludeRes(List<ProcessExcludeResRule> list) throws Exception {
        baseDao.batchSave(list);
        for (ProcessExcludeResRule p : list) {
            processExcludeResRuleCache.put(p);
        }
    }
}
